package com.ba.controller;

import com.ba.pojo.Card;
import com.ba.pojo.Player;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Play {
    List<Card> box = new ArrayList<>(54);
    public void start() {
        prepare(); // 准备牌
        publish(); // 发牌
        show(); // 展示牌
        System.out.println("发牌程序结束喽！");
    }

    private void prepare() {
        for(int i = 1; i < 14 ; i++) {
            String str_num = String.valueOf(i);
            int t_num = i;
            switch (i) {
                case 1:
                    str_num = "A";
                    t_num = 14;
                    break;
                case 2:
                    t_num = 15;
                    break;
                case 11:
                    str_num = "J";
                    break;
                case 12:
                    str_num = "Q";
                    break;
                case 13:
                    str_num = "K";
                    break;
            }
            Card c1 = create("♠", str_num, t_num);
            Card c2 = create("♥", str_num, t_num);
            Card c3 = create("♣", str_num, t_num);
            Card c4 = create("♦", str_num, t_num);

            box.add(c1);
            box.add(c2);
            box.add(c3);
            box.add(c4);
        }

        box.add(create("joke", "", 88));
        box.add(create("JOKE", "", 888));

    }

    private Card create(String pattern, String str_num, int t_size) {
        return new Card(pattern, str_num, t_size);
    }

    private Player p1, p2, p3;
    private void publish() {
        p1 = new Player("John");
        p2 = new Player("Jane");
        p3 = new Player("Bob");
        for (int i = 0; i < 54; i++) {
            int pos = new Random().nextInt(box.size());
            Card get = box.remove(pos);
            int which = i % 3;
            switch (which) {
                case 0:
                    p1.receiveCard(get);
                    break;
                case 1:
                    p2.receiveCard(get);
                    break;
                default:
                    p3.receiveCard(get);
            }
        }
    }

    private void show() {
        p1.showCards();
        p2.showCards();
        p3.showCards();
    }
}
